import json

from django.http import JsonResponse
from rest_framework.response import Response

from rest_framework.views import APIView


from apps.ten_auth.models import *

# Create your views here.

# 活动列表
class Activity(APIView):
    def get(self, request):
        huoqu = Gathering.objects.all()
        list = []
        for i in huoqu:
            list.append({
                'id': i.id,
                'name': i.name,
                'image':str(i.image),
                'city': i.city,
                'starttime': i.starttime,
                'endrolltime': i.endrolltime,
                'users': [i.id for i in i.users.all()]
            })
        return Response({
            'code':200,
            'msg':'获取活动列表成功',
            'results': list
        })
# 活动详情
class Details(APIView):
    def get(self,request,g_id):
        try:
            huoqu = Gathering.objects.get(id=g_id)
        except Exception as e:
            return Response({'code':400,'errmsg':'不存在这个活动'})

        return Response({
                'id':huoqu.id,
                'name':huoqu.name,
                'summary':huoqu.summary,
                'detail':huoqu.detail,
                'address':huoqu.address,
                'sponsor':huoqu.sponsor,
                'image':"http://s5ykralt6.hn-bkt.clouddn.com/" + str(huoqu.image),
                'city':huoqu.city,
                'state':huoqu.state,
                'starttime':huoqu.starttime,
                'endtime':huoqu.endtime,
                'endrolltime':huoqu.endrolltime,
                'users':[i.id for i in huoqu.users.all()],
        })

from django.utils.decorators import method_decorator
from utils.simmiddleware01 import login_required_my

# 报名活动
class Application(APIView):
    @method_decorator(login_required_my, name='put')
    def post(self, request, g_id):
        u_id = request.session.id
        huoqu = Gathering.objects.filter(id=g_id).first()
        if huoqu.users.filter(id=u_id).first():
            huoqu.users.remove(User.objects.get(id=u_id))
            huoqu.save()
            return Response({'message': '取消报名！', 'success': True})
        huoqu.users.add(u_id)
        huoqu.save()
        return Response({'message': '报名成功！', 'success': True})
<<<<<<< HEAD
=======









>>>>>>> 35c645a189cd9176b6187ea8c3cdd5e83499bb79
"""———————————————————————————————————————————————————《吐槽》—————————————————————————————————————————"""

from bson import ObjectId
from bs4 import BeautifulSoup
from datetime import datetime

# 吐槽列表
class Tabulation(APIView):
    def get(self, request):
        spits = Spit.objects.all()
        list = []
        for i in spits:
            list.append({
                'id': i.id,
                'content': i.content,
                'publishtime': str(i.publishtime),
                'userid': i.userid,
                'nickname': i.nickname,
                'visits': int(i.visits),
                'thumbup': int(i.thumbup),
                'share': 0,
                'comment': int(i.comment),
                'avatar':0,
                'collected': i.collected,
                'hasthumbup': i.hasthumbup,
                'parent': 0
            })
        return Response(list)
    # 发布吐槽
    def post(self, request):
        user_id = request.session.id
        data = json.loads(request.body)
        content = data['content']
        suop = BeautifulSoup(content, 'html.parser')
        content_text = suop.get_text()
        spit = Spit(
            content=content_text,
            publishtime=datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
            userid='1',
            nickname='杨豪楠',
            visits=0,
            thumbup=0,
            comment=0,
            avatar=0,
            collected=False,
            hasthumbup=False,
            parent=None
        )
        spit.save()
        return JsonResponse({'message': '发布吐槽成功', 'success': True})

# 收藏取消收藏
class Collect(APIView):
    def put(self,request,id):
        try:
            huoqu = Spit.objects.filter(id=id).first()
        except Exception as e:
            print(e)
            return JsonResponse({'success': False, 'message': '数据错误'})
        if huoqu.collected == False:
            huoqu.collected = True
            huoqu.visits += 1
            huoqu.save()
            return JsonResponse({'message': '收藏成功', 'success': True})
        else:
            huoqu.collected = False
            huoqu.visits -= 1
            huoqu.save()
            return JsonResponse({'success': True, 'message': '取消收藏成功'})

# 点赞取消点赞
class Like(APIView):
    def put(self, request, id):
        try:
            huoqu = Spit.objects.filter(id=id).first()
        except Exception as e:
            print(e)
            return JsonResponse({'success': False, 'message': '数据错误'})
        if huoqu.hasthumbup == False:
            huoqu.hasthumbup = True
            huoqu.thumbup += 1
            huoqu.save()
            return JsonResponse({'message': '点赞成功', 'success': True})
        else:
            huoqu.hasthumbup = False
            huoqu.thumbup -= 1
            huoqu.save()
            return JsonResponse({'success': True, 'message': '取消点赞成功'})

# 吐槽详情
class Roast(APIView):
    def get(self, reqquest,id):
            huoqu = Spit.objects.get(id=id)
            list = {
                'content': huoqu.content,
                'publishtime': huoqu.publishtime,
                'userid': huoqu.userid,
                'nickname': huoqu.nickname,
                'visits': huoqu.visits,
                'thumbup': huoqu.thumbup,
                'comment': huoqu.comment,
                'collected': huoqu.collected,
                'hasthumbup': huoqu.hasthumbup,
                'parent': huoqu.parent
            }
            return Response(list)
# 获取吐槽评论
class Comment(APIView):
    def get(self, request, id):
        try:
            spii = Spit.objects.filter(parent=id).all()
            data_list = []
            for spit in spii:
                data_list.append({
                    'id': str(ObjectId(id)),
                    'content': spit.content,
                    'publishtime': spit.publishtime,
                    'userid': spit.userid,
                    'nickname': spit.nickname,
                    'visits': spit.visits,
                    'thumbup': spit.thumbup,
                    'comment': spit.comment,
                    'avatar': spit.avatar,
                    'collected': spit.collected,
                    'hasthumbup': spit.hasthumbup,
                    'parent': spit.parent
                })
            return JsonResponse(data_list, safe=False)
        except Exception as e:
            return JsonResponse({'errmsg': str(e)})